"""
Case Type   : 堆表多块预读
Case Name   : alter user/database set 方式设置heap_bulk_read_size参数
Create At   : 2024/01/23
Owner       : songjing20
Description :
    1.创建用户和数据库
    2.alter user set方式设置,查询参数值
    3.alter user set方式设置错误值
    4.alter database set方式设置,查询参数值
    5.alter database set方式设置无效值
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.报错
    4.成功
    5.报错
    6.成功
History     :
"""
import os
import unittest

from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class MultiBulkRread(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.commsh_pri = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'heap_bulk_read_size'
        self.user = "u_pre_read_bulk_0002"
        self.db_name = "db_pre_read_bulk_0002"
        self.user_param = f"-U {self.user} -W{macro.COMMON_PASSWD}"
        self.values = (1, 32, 64, 0, "'512kB'", "'8kB'", "'0kB'")
        self.err_values = (-1, 65, "''", 111111111111, "'//'", "'#'")

    def test_multi_bulk_read(self):
        text1 = '-----step1:创建用户和数据库;expect:成功-----'
        self.log.info(text1)
        sql = f"drop user if exists {self.user} cascade;" \
              f"drop database if exists {self.db_name};" \
              f"create user {self.user} password '{macro.COMMON_PASSWD}';" \
              f"create database {self.db_name};"
        sql_res = self.commsh_pri.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res,
                      f'执行失败:{text1}')
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      f'执行失败:{text1}')

        text2 = '-----step2:alter user set方式设置,查询参数值;expect:成功-----'
        self.log.info(text2)
        for value in self.values:
            self.log.info(f"-----设置为：{value} -----")
            res = self.commsh_pri.execut_db_sql(
                f"alter user {self.user} set {self.param} = {value}")
            self.log.info(res)
            self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, res,
                          f"执行失败:{text2}")
            res = self.commsh_pri.execut_db_sql(
                f'show {self.param};', self.user_param).splitlines()[-2]
            self.log.info(res)
            if isinstance(value, int) or '0kB' in value:
                self.assertTrue(f"{value * 8}kB" in res or '0' == res.strip(),
                                f'执行失败:{text2}')
            else:
                self.assertIn(value.strip("'"), res, f'执行失败:{text2}')

        text3 = '-----step3:alter user set方式设置错误值;expect:合理报错-----'
        self.log.info(text3)
        for value in self.err_values:
            self.log.info(f"-----设置为：{value} -----")
            res = self.commsh_pri.execut_db_sql(
                f"alter user {self.user} set {self.param} = {value}")
            self.log.info(res)
            self.assertIn('ERROR', res, f'执行失败:{text3}')

        text4 = '-----step4:alter database set方式设置,查询参数值;expect:成功-----'
        self.log.info(text4)
        for value in self.values:
            self.log.info(f"-----设置为：{value} -----")
            res = self.commsh_pri.execut_db_sql(
                f"alter database {self.db_name} set {self.param} = {value}")
            self.log.info(res)
            self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, res,
                          f"执行失败:{text4}")
            res = self.commsh_pri.execut_db_sql(
                f'show {self.param};', dbname=self.db_name).splitlines()[-2]
            self.log.info(res)
            if isinstance(value, int) or '0kB' in value:
                self.assertTrue(f"{value * 8}kB" in res or '0' == res.strip(),
                                f'执行失败:{text2}')
            else:
                self.assertIn(value.strip("'"), res, f'执行失败:{text2}')

        text5 = '-----step5:alter database set方式设置错误值;expect:合理报错-----'
        self.log.info(text5)
        for value in self.err_values:
            self.log.info(f"-----设置为：{value} -----")
            res = self.commsh_pri.execut_db_sql(
                f"alter database {self.db_name} set {self.param} = {value}")
            self.log.info(res)
            self.assertIn('ERROR', res, f'执行失败:{text3}')

    def tearDown(self):
        text = '-----step6:清理环境;expect:成功-----'
        self.log.info(text)
        res = self.commsh_pri.execut_db_sql(
            f"drop user {self.user} cascade;"
            f"drop database {self.db_name};")
        self.log.info(res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, res, f'执行失败:{text}')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
